
import socket, msvcrt, sys
import win32com.client
from tempfile import TemporaryFile
from time import sleep as sleep_sec

theport = int(sys.argv[1]) if len(sys.argv) > 1 else int(8080)
print "port: ", theport
thesock = socket.socket( socket.AF_INET, socket.SOCK_DGRAM, socket.IPPROTO_UDP )
thesock.setsockopt( socket.SOL_SOCKET, socket.SO_REUSEADDR, 1 )
#   thesock.setsockopt( socket.SOL_SOCKET, socket.SO_REUSEPORT, 1)
#   thesock.setsockopt( socket.SOL_SOCKET, socket.SO_SNDBUF, 1024 * 100 )

theobj = win32com.client.Dispatch("oleauto.screenshot")
theobj.pixelBits = 16   #   theobj.cursor, 
with TemporaryFile(suffix=".bmp", dir = r".") as thefile: 
    thenum = thefile.fileno() 
    thereply = theobj.load(msvcrt.get_osfhandle(thenum)) # , True)
    sleep_sec(0) # retval = theobj.length
    thefile.seek(0)
    thebuffer = thefile.read(20 * 1024)
    while thebuffer :
        thesock.sendto( thebuffer, ("192.168.1.255", theport) )
        thebuffer = thefile.read(20 * 1024)
#   print "result: ", thesock.getsockopt( socket.SOL_SOCKET, socket.SO_SNDBUF )
thesock.close()
#   thesock.bind( ("192.168.1.255", theport) )
#   thefile = thesock.makefile('w');
#   thefile.close()
print "have finished"
